package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.service.NjtUserRegisterService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nongjt/userRegister")
public class NjtUserRegisterController extends BaseController {
    @Autowired
    private NjtUserRegisterService njtUserRegisterService;

    @GetMapping("/list")
    public TableDataInfo list(NjtUserRegister njtUserRegister){
        startPage();
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(njtUserRegister);
        return getDataTable(list);
    }
    @PutMapping
    public AjaxResult update(@RequestBody NjtUserRegister njtUserRegister){
        int rows = njtUserRegisterService.updateNjtUserRegister(njtUserRegister);
        return toAjax(rows);
    }

    @DeleteMapping("deleteByUserId")
    public AjaxResult delete(@RequestParam Integer userId){
        int rows = njtUserRegisterService.deleteNjtUserRegisterById(userId);
        return toAjax(rows);
    }

    @PostMapping("/export")
    public void export(NjtUserRegister njtUserRegister, HttpServletResponse  response){
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(njtUserRegister);
        ExcelUtil<NjtUserRegister> util = new ExcelUtil<NjtUserRegister>(NjtUserRegister.class);
        util.exportExcel(response, list, "用户注册信息");
    }
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestParam("userIds") String userIds) {
        try {
            String[] idArray = userIds.split(",");
            Long[] userIdArray = new Long[idArray.length];
            for (int i = 0; i < idArray.length; i++) {
                userIdArray[i] = Long.valueOf(idArray[i]);
            }
            int result = njtUserRegisterService.deleteNjtUserRegisterByIds(userIdArray);
            return toAjax(result);
        } catch (Exception e) {
            logger.error("批量删除用户失败", e);
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
    }
}
